

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="什么是对象可以把对象具象成一个实际的物体、人、工具。 可以拥有：  属性 行为  属性可以理解为，在对象内部公用的一些变量值 行为可以理解为，在对象内部公用的一些函数方法 当然，属性和行为，都可以被外界所使用 不为外界所使用的，我们把它称为私有属性、私有行为 123456789101112131415161718const mrMu &#x3D; &amp;#123;    name: &amp;#x27;muzhou&amp;#">
<meta property="og:type" content="article">
<meta property="og:title" content="对象精讲">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2022/10/09/notes/js/object/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="什么是对象可以把对象具象成一个实际的物体、人、工具。 可以拥有：  属性 行为  属性可以理解为，在对象内部公用的一些变量值 行为可以理解为，在对象内部公用的一些函数方法 当然，属性和行为，都可以被外界所使用 不为外界所使用的，我们把它称为私有属性、私有行为 123456789101112131415161718const mrMu &#x3D; &amp;#123;    name: &amp;#x27;muzhou&amp;#">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-10-09T08:16:59.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.257Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="测试平台">
<meta property="article:tag" content="javascript">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>对象精讲 - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="对象精讲"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-10-09 16:16" pubdate>
          2022年10月9日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.7k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">对象精讲</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="什么是对象"><a href="#什么是对象" class="headerlink" title="什么是对象"></a>什么是对象</h1><p>可以把对象具象成一个实际的物体、人、工具。</p>
<p>可以拥有：</p>
<ul>
<li>属性</li>
<li>行为</li>
</ul>
<p>属性可以理解为，在对象内部公用的一些变量值</p>
<p>行为可以理解为，在对象内部公用的一些函数方法</p>
<p>当然，属性和行为，都可以被外界所使用</p>
<p>不为外界所使用的，我们把它称为<strong>私有属性、私有行为</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> mrMu = &#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;muzhou&#x27;</span>,<br>    <span class="hljs-attr">age</span>: <span class="hljs-number">80</span>,<br>    <span class="hljs-attr">sex</span>: <span class="hljs-string">&#x27;male&#x27;</span>,<br>    <span class="hljs-attr">power</span>: <span class="hljs-number">100</span>,<br>    <span class="hljs-attr">run</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;我在跑步&quot;</span>);<br>        mrMu.<span class="hljs-property">power</span> --;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`体力下降了，目前体力<span class="hljs-subst">$&#123;mrMu.power&#125;</span>`</span>);<br>    &#125;,<br>    <span class="hljs-attr">sleep</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;我在睡觉&quot;</span>);<br>        mrMu.<span class="hljs-property">power</span> ++;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`体力增加了，目前体力<span class="hljs-subst">$&#123;mrMu.power&#125;</span>`</span>);<br>    &#125;<br>&#125;<br>mrMu.<span class="hljs-title function_">run</span>()<br>mrMu.<span class="hljs-title function_">sleep</span>()<br></code></pre></td></tr></table></figure>

<h1 id="this"><a href="#this" class="headerlink" title="this"></a>this</h1><p>这里引入了一个概念<strong>this</strong></p>
<p>this的使用，需要结合使用环境，以及函数声明方式来决定，后续会有章节详细介绍。</p>
<p>在下述列子中，this就是指向自己</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> mrMu = &#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;muzhou&#x27;</span>,<br>    <span class="hljs-attr">age</span>: <span class="hljs-number">80</span>,<br>    <span class="hljs-attr">sex</span>: <span class="hljs-string">&#x27;male&#x27;</span>,<br>    <span class="hljs-attr">power</span>: <span class="hljs-number">100</span>,<br>    <span class="hljs-attr">run</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;我在跑步&quot;</span>);<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">power</span> --;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`体力下降了，目前体力<span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.power&#125;</span>`</span>);<br>    &#125;,<br>    <span class="hljs-attr">sleep</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;我在睡觉&quot;</span>);<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">power</span> ++;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`体力增加了，目前体力<span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.power&#125;</span>`</span>);<br>    &#125;<br>&#125;<br>mrMu.<span class="hljs-title function_">run</span>()<br>mrMu.<span class="hljs-title function_">sleep</span>()<br></code></pre></td></tr></table></figure>

<h1 id="对象的增删改查"><a href="#对象的增删改查" class="headerlink" title="对象的增删改查"></a>对象的增删改查</h1><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> mrMu = &#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;muzhou&#x27;</span>,<br>    <span class="hljs-attr">age</span>: <span class="hljs-number">80</span>,<br>    <span class="hljs-attr">sex</span>: <span class="hljs-string">&#x27;male&#x27;</span>,<br>    <span class="hljs-attr">power</span>: <span class="hljs-number">100</span>,<br>    <span class="hljs-attr">run</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;我在跑步&quot;</span>);<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">power</span> --;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`体力下降了，目前体力<span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.power&#125;</span>`</span>);<br>    &#125;,<br>    <span class="hljs-attr">sleep</span>: <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&quot;我在睡觉&quot;</span>);<br>        <span class="hljs-variable language_">this</span>.<span class="hljs-property">power</span> ++;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`体力增加了，目前体力<span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.power&#125;</span>`</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-comment">// 新增和修改，代码书写方式一样，遵循有则覆盖，无则新增的规则</span><br>mrMu.<span class="hljs-property">name</span> = <span class="hljs-string">&#x27;muzhou2&#x27;</span> <span class="hljs-comment">// 修改</span><br>mrMu.<span class="hljs-property">height</span> = <span class="hljs-number">185</span>  <span class="hljs-comment">// 新增</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(mrMu.<span class="hljs-property">height</span>);  <span class="hljs-comment">// 查询</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(mrMu.<span class="hljs-property">name</span>);  <span class="hljs-comment">// 查询</span><br><br><span class="hljs-keyword">delete</span> mrMu.<span class="hljs-property">sex</span>  <span class="hljs-comment">// 删除</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(mrMu.<span class="hljs-property">sex</span>);<br></code></pre></td></tr></table></figure>

<h1 id="对象的创建-构造函数"><a href="#对象的创建-构造函数" class="headerlink" title="对象的创建-构造函数"></a>对象的创建-构造函数</h1><p>上述的对象的创建方式，称为<strong>字面量</strong>，也是我们最常用的声明方式。</p>
<p>下面介绍另一种方式：<strong>构造函数</strong></p>
<p>通过关键字<strong>new</strong>来创建一个对象</p>
<h2 id="系统自带的构造函数"><a href="#系统自带的构造函数" class="headerlink" title="系统自带的构造函数"></a>系统自带的构造函数</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">const</span> num = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Number</span>()<br><span class="hljs-keyword">const</span> str = <span class="hljs-keyword">new</span> <span class="hljs-title class_">String</span>()<br><span class="hljs-keyword">const</span> lst = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Array</span>()<br><span class="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Object</span>()<br><span class="hljs-comment">// ......</span><br></code></pre></td></tr></table></figure>

<h2 id="自定义构造函数"><a href="#自定义构造函数" class="headerlink" title="自定义构造函数"></a>自定义构造函数</h2><p>自定义对象是，命名需要按照<strong>大驼峰</strong>的方式来命名 </p>
<blockquote>
<p>FirstName</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age</span>)&#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">say</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`我叫<span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.name&#125;</span>`</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;muzhou&#x27;</span>, <span class="hljs-number">18</span>)<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">name</span>);<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">age</span>);<br>person.<span class="hljs-title function_">say</span>()<br></code></pre></td></tr></table></figure>

<h2 id="构造函数工作原理"><a href="#构造函数工作原理" class="headerlink" title="构造函数工作原理"></a>构造函数工作原理</h2><p>只要使用new关键字来进行构造函数对象的创建，都会进行三步隐式转化：</p>
<ul>
<li>创建this对象</li>
<li>执行this代码</li>
<li>return this</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-comment">// 隐式创建this = &#123;&#125;</span><br><br>    <span class="hljs-comment">// 执行this代码</span><br><br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">say</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`我叫<span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.name&#125;</span>`</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// 隐式return this</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="包装类的隐式执行"><a href="#包装类的隐式执行" class="headerlink" title="包装类的隐式执行"></a>包装类的隐式执行</h2><p>我们要明确一点，只有 <strong>引用</strong> 对象才有属性。</p>
<p>换句话说，原始值是没有属性的，也就是说：字符串、数字、小数、boolean等原始值是没有属性的。</p>
<p>但是js会在我们使用方式不对时，隐式执行：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><br><span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;<br><br>num.<span class="hljs-property">test</span> = <span class="hljs-string">&#x27;123&#x27;</span> <span class="hljs-comment">// 不会报错</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num.<span class="hljs-property">test</span>) <span class="hljs-comment">// undefined</span><br></code></pre></td></tr></table></figure>

<p>哎？上面不是说，不能原始值不能有属性吗？那为什么没有报错呢？</p>
<p>这是因为JavaScript帮我们做了隐式运行。</p>
<p>实际上的运行过程：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span>;<br><br><span class="hljs-comment">// 1、num = new Number(num)</span><br><span class="hljs-comment">// 2、num.test = 123</span><br><span class="hljs-comment">// 3、delete num，重新创建一个num</span><br>num.<span class="hljs-property">test</span> = <span class="hljs-string">&#x27;123&#x27;</span> <br><br><span class="hljs-comment">// 1、num = new Number（num）</span><br><span class="hljs-comment">// 2、输出num.test</span><br><span class="hljs-comment">// 3、delete num，重新创建一个num</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num.<span class="hljs-property">test</span>) <br></code></pre></td></tr></table></figure>

<p>所以，JavaScript是很灵活，同时也很恶心的代码。明明代码可以执行，但是结果却不是我们肉眼看到判断出来的结果。</p>
<p>庆幸的是，实际开发中，并不会这么使用。</p>
<h1 id="原型"><a href="#原型" class="headerlink" title="原型"></a>原型</h1><p>原型是对象中很经典，也是很重要的知识。</p>
<p>在前端开发的面试中，原型链几乎已经成为必问的知识点。</p>
<p>虽然现在已经很少使用原型相关知识，但是它已经成为基础搭建中必不可少的部分。</p>
<h2 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h2><ul>
<li>原型也是对象</li>
<li>所有对象都有上级原型</li>
<li>Object.prototype在大多情况下最终原型</li>
<li>通过构造函数创建的对象，可以继承原型的属性和方法</li>
<li>原型之间存在链式链接，可以提取同名属性</li>
<li>可以通过对象的隐式属性 <strong><strong>proto</strong></strong> 来查看原型</li>
</ul>
<h2 id="构造函数的原型属性"><a href="#构造函数的原型属性" class="headerlink" title="构造函数的原型属性"></a>构造函数的原型属性</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><br><span class="hljs-comment">// prototype，系统自带属性，可以通过该属性给原型增加对象</span><br><span class="hljs-comment">// 换句话说。prototype就是咱们所说的原型</span><br><span class="hljs-comment">// 原型本身就是一个对象，在构造函数声明时，他就被创建好了</span><br><span class="hljs-comment">// 初始化：Person.prototype = &#123;&#125;</span><br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">name</span> = <span class="hljs-string">&#x27;muzhou&#x27;</span> <br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br><br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person) <span class="hljs-comment">// &#123;&#125;空对象</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">name</span>) <span class="hljs-comment">// muzhou 继承了原型的属性。通过原型链， 提取同名属性</span><br><br><span class="hljs-keyword">var</span> person1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person1) <span class="hljs-comment">// &#123;&#125;空对象</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person1.<span class="hljs-property">name</span>)  <span class="hljs-comment">//  muzhou 继承同一个原型，所以原型链向同一个原型</span><br><br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">name</span> = <span class="hljs-string">&#x27;muzhou2&#x27;</span> <br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">name</span>);  <span class="hljs-comment">// muzhou2</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person1.<span class="hljs-property">name</span>);  <span class="hljs-comment">// muzhou2</span><br><br></code></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;木&#x27;</span><br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name</span>)&#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br><br>    <span class="hljs-comment">// lastName继承原型</span><br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">say</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`我的名字叫<span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.lastName&#125;</span><span class="hljs-subst">$&#123;<span class="hljs-variable language_">this</span>.name&#125;</span>`</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-keyword">var</span> person1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;舟1&#x27;</span>)<br><span class="hljs-keyword">var</span> person2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;舟2&#x27;</span>)<br><br>person1.<span class="hljs-title function_">say</span>() <span class="hljs-comment">// 我的名字叫木舟1</span><br>person2.<span class="hljs-title function_">say</span>() <span class="hljs-comment">// 我的名字叫木舟2</span><br><br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;mu&#x27;</span><br><br>person1.<span class="hljs-title function_">say</span>() <span class="hljs-comment">//  我的名字叫mu舟1</span><br>person2.<span class="hljs-title function_">say</span>()  <span class="hljs-comment">// 我的名字叫mu舟2</span><br></code></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 原型链提取属性时，如果当前对象有自己的属性，则以自己的属性优先</span><br><br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;木&#x27;</span><br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;mu&#x27;</span><br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">lastName</span>); <span class="hljs-comment">// mu</span><br></code></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 原型对象一次性赋值</span><br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = &#123;<br>    <span class="hljs-attr">lastName</span>: <span class="hljs-string">&#x27;mu&#x27;</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;zhou&#x27;</span><br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">lastName</span>);<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">name</span>);<br></code></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// 对象不可直接修改原型的属性</span><br><br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = &#123;<br>    <span class="hljs-attr">lastName</span>: <span class="hljs-string">&#x27;mu&#x27;</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;zhou&#x27;</span><br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">lastName</span>);<br><br>person.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;木&#x27;</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">lastName</span>); <span class="hljs-comment">// mu</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">lastName</span>);  <span class="hljs-comment">// 木  我们之前说过，以当前对象优先，person.lastName 实际上是给自己本身的属性进行赋值或修改</span><br></code></pre></td></tr></table></figure>

<h2 id="proto"><a href="#proto" class="headerlink" title="proto"></a>proto</h2><p><code>__proto__</code>是对象的隐式属性，指向原型</p>
<p>所以通过该属性，可以直接修改原型的属性</p>
<p>但这不意味着对象可以修改原型。属性的修改，以当前的对象优先。</p>
<p>proto 从本质意义上，并不属于对象的属性，它只是js用来寻找原型链的入口。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = &#123;<br>    <span class="hljs-attr">lastName</span>: <span class="hljs-string">&#x27;mu&#x27;</span>,<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;zhou&#x27;</span><br>&#125;<br><br><span class="hljs-comment">// 构造函数在 new 之后，隐式创建__proto__，指向this</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-comment">// this = &#123;__proto__: Person.prototype&#125;</span><br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-keyword">var</span> person1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">__proto__</span>); <span class="hljs-comment">// &#123;lastName: &#x27;mu&#x27;, name: &#x27;zhou&#x27;&#125;</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person1.<span class="hljs-property">lastName</span>); <span class="hljs-comment">// mu</span><br><br>person.<span class="hljs-property">__proto__</span>.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;木&#x27;</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">lastName</span>);  <span class="hljs-comment">// 木</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person1.<span class="hljs-property">lastName</span>);  <span class="hljs-comment">// 木</span><br></code></pre></td></tr></table></figure>


<h2 id="工厂化编程"><a href="#工厂化编程" class="headerlink" title="工厂化编程"></a>工厂化编程</h2><p>就相当于python中的class（JavaScript中的新语法，支持class声明，后续会有说明）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><br><span class="hljs-title class_">Car</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">brand</span> = <span class="hljs-string">&quot;奔驰&quot;</span><br><span class="hljs-title class_">Car</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">height</span> = <span class="hljs-number">2000</span><br><span class="hljs-title class_">Car</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">width</span> = <span class="hljs-number">4000</span><br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Car</span>(<span class="hljs-params">ownerName, price, color</span>)&#123;<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">color</span> = color<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">ownerName</span> = ownerName<br>    <span class="hljs-variable language_">this</span>.<span class="hljs-property">price</span> = price<br>&#125;<br><br><span class="hljs-keyword">var</span> myCar = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Car</span>(<span class="hljs-string">&#x27;muzhou&#x27;</span>, <span class="hljs-string">&#x27;20w&#x27;</span>, <span class="hljs-string">&#x27;grey&#x27;</span>)<br><br><span class="hljs-keyword">var</span> yourCar = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Car</span>(<span class="hljs-string">&#x27;yourName&#x27;</span>, <span class="hljs-string">&#x27;19w&#x27;</span>, <span class="hljs-string">&#x27;blue&#x27;</span>)<br></code></pre></td></tr></table></figure>

<h2 id="整点有意思的"><a href="#整点有意思的" class="headerlink" title="整点有意思的"></a>整点有意思的</h2><p>这一小节涉及到 <strong>引用</strong>，具体看代码</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">name</span> = <span class="hljs-string">&#x27;木&#x27;</span><br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-comment">// this = &#123;__proto__: Person.prototype&#125;</span><br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = &#123;<br>    <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;mu&#x27;</span><br>&#125;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">name</span>);  <span class="hljs-comment">// 木</span><br></code></pre></td></tr></table></figure>

<p>哎？有意思的就来了，为什么结果是 木 而不是 mu ？</p>
<p>可以看到，构造函数在new之后，proto就被赋值引用到原型的内存地址中</p>
<p>所以在 person new 之后，person的原型，已经指定到一个内存空间了</p>
<p>这个时候，把Person.prototype重新赋值，相当于给予一个新的内存空间。</p>
<p>而 person 对象原本的proto执行的内存空间不变。</p>
<p>所以 person.name 实际上，是调用的之前的内存空间中的name。</p>
<p>再来整点有意思的</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">num</span> = <span class="hljs-number">100</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params"></span>)&#123;<br><br>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><span class="hljs-keyword">var</span> person1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>()<br><br>person.<span class="hljs-property">num</span> ++  <span class="hljs-comment">// 实际上执行的是 person.num = person.num + 1</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">num</span>);  <span class="hljs-comment">// 101  </span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person1.<span class="hljs-property">num</span>);  <span class="hljs-comment">// 100</span><br></code></pre></td></tr></table></figure>

<h2 id="原型链"><a href="#原型链" class="headerlink" title="原型链"></a>原型链</h2><p>之前说过，原型和原型之间，存在链式链接，大多会链接到Object</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><br><span class="hljs-comment">// Grand原型的proto指向的就是Object.prototype</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Grand</span>(<span class="hljs-params"></span>)&#123;<br><br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Parent</span>(<span class="hljs-params"></span>)&#123;<br><br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Son</span>(<span class="hljs-params"></span>)&#123;<br><br>&#125;<br><br><span class="hljs-title class_">Grand</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;木&#x27;</span><br><br><span class="hljs-keyword">var</span> grand = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Grand</span>()<br><br><span class="hljs-title class_">Parent</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = grand<br><br><span class="hljs-keyword">var</span> parent = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Parent</span>()<br><br><span class="hljs-title class_">Son</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = parent<br><br><span class="hljs-keyword">var</span> son = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Son</span>()<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(son.<span class="hljs-property">lastName</span>); <span class="hljs-comment">// 木，lastName从grand中找到</span><br><br>parent.<span class="hljs-property">lastName</span> = <span class="hljs-string">&#x27;mu&#x27;</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(son.<span class="hljs-property">lastName</span>); <span class="hljs-comment">// mu，lastName从parent中找到</span><br><br><span class="hljs-title class_">Object</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">testName</span> = <span class="hljs-string">&#x27;muzhou&#x27;</span> <span class="hljs-comment">// 谨慎使用，一般直接修改Object原型，是需要增加一个共用函数</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(son.<span class="hljs-property">testName</span>);  <span class="hljs-comment">// muzhou testName从最终的Object中找到</span><br><br></code></pre></td></tr></table></figure>

<h2 id="原始值的toString"><a href="#原始值的toString" class="headerlink" title="原始值的toString"></a>原始值的toString</h2><p>之前说过，原始值是没有 属性 的</p>
<p>那为何会有toSting？</p>
<p>这就涉及到之前讲过的 <strong>包装类</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-keyword">var</span> num = <span class="hljs-number">123</span><br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num.<span class="hljs-title function_">toSting</span>())<br><br><span class="hljs-comment">// 调用toString时，会隐式执行 num = new Number(num)</span><br><span class="hljs-comment">// 所以这个时候的num.toString，实际上使用的是Number的toSting方法</span><br><br><span class="hljs-title class_">Number</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">toString</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>    <span class="hljs-keyword">return</span> <span class="hljs-string">&#x27;哎我就是玩儿&#x27;</span><br>&#125;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(num.<span class="hljs-title function_">toString</span>());  <span class="hljs-comment">// 哎我就是玩儿</span><br><br></code></pre></td></tr></table></figure>

<p>所以同样的，其他的原始值，调用不存在它本身的方法时，会隐式执行包装类原型上的方法。</p>
<blockquote>
<p>只有undefined和null，不能调用toString</p>
</blockquote>
<h2 id="Object-create"><a href="#Object-create" class="headerlink" title="Object.create"></a>Object.create</h2><p>按照我们上面所说的，<strong>原型链大多最终会指向Object.prototype</strong></p>
<p>那么，其他情况是什么？</p>
<p>这是之前阿里还是百度的一道面试题</p>
<blockquote>
<p>所有对象的原型，最终都会指向Object吗？</p>
</blockquote>
<p>答案是否</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">// Object.create接受一个参数，要求该参数必须是一个对象或者是null</span><br><span class="hljs-comment">// 当我们传null时，原型的最终指向就不是Object</span><br><br><span class="hljs-keyword">var</span> obj = &#123;<span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;muzhou&#x27;</span>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj)<br><br><span class="hljs-keyword">var</span> obj = &#123;<span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;muzhou&#x27;</span>&#125;<br><br><span class="hljs-keyword">var</span> person = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(obj)<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(person.<span class="hljs-property">name</span>);<br><br><span class="hljs-keyword">var</span> personNull = <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">create</span>(<span class="hljs-literal">null</span>)<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(personNull);  <span class="hljs-comment">// No properties</span><br></code></pre></td></tr></table></figure>
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/" class="category-chain-item">测试开发</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/javascript/" class="category-chain-item">javascript</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%B9%B3%E5%8F%B0/">#测试平台</a>
      
        <a href="/my-blog/tags/javascript/">#javascript</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>对象精讲</div>
      <div>https://walker_kwu.gitee.io/my-blog/2022/10/09/notes/js/object/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年10月9日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2022/10/17/devops/docker/" title="docker -- 持续集成必备工具">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">docker -- 持续集成必备工具</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2022/10/05/python/useful/func/" title="python实用代码">
                        <span class="hidden-mobile">python实用代码</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
